﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AutoLoL.Entities;
using AutoLoL.Logic;
using AutoLoL.Logic.Factories;

namespace AutoLoL.Dialogs
{
    /// <summary>
    /// Interaction logic for SummonerSpellDialog.xaml
    /// </summary>
    public partial class SummonerSpellDialog : Window
    {
        public SummonerSpell SelectedSpell { get; private set; }

        private SummonerSpell[] _improved;

        public SummonerSpellDialog(Window owner, SummonerSpell[] improved = null)
        {
            InitializeComponent();

            Owner = owner;

            CancelButton.IsDefault = true;
            CancelButton.IsCancel = true;

            ReadOnlyCollection<SummonerSpell> summonerSpells = SummonerSpellFactory.Instance.GetAll(MasteryFactory.Instance);
            SpellsItemsControl.ItemsSource = summonerSpells;

            _improved = improved;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SummonerSpellIterator((SummonerSpell summonerSpell, Image image, Rectangle rectangle) =>
            {
                if (_improved.Contains(summonerSpell))
                {
                    rectangle.Visibility = Visibility.Collapsed;
                }
            });
        }

        private void Spell_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SummonerSpellIterator((SummonerSpell summonerSpell, Image image, Rectangle rectangle) =>
            {
                if (image == sender || rectangle == sender)
                {
                    SelectedSpell = summonerSpell;
                    Close();
                }
            });
        }

        private void NoneButton_Click(object sender, RoutedEventArgs e)
        {
            // Ugly, but oh well =)
            SelectedSpell = new SummonerSpell() { Name = "None" };
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            SelectedSpell = null;
            Close();
        }

        private void SummonerSpellIterator(Action<SummonerSpell, Image, Rectangle> action)
        {
            foreach (SummonerSpell summonerSpell in SpellsItemsControl.Items)
            {
                ContentPresenter cp = SpellsItemsControl.ItemContainerGenerator.ContainerFromItem(summonerSpell) as ContentPresenter;
                if (cp != null)
                {
                    DependencyObject gridChild = VisualTreeHelper.GetChild(cp, 0);
                    if (gridChild != null)
                    {
                        DependencyObject imageChild = VisualTreeHelper.GetChild(gridChild, 0);
                        DependencyObject rectangleChild = VisualTreeHelper.GetChild(gridChild, 1);
                        if (imageChild != null && rectangleChild != null)
                        {
                            Image image = imageChild as Image;
                            Rectangle rectangle = rectangleChild as Rectangle;

                            if (image != null && rectangle != null)
                            {
                                action(summonerSpell, image, rectangle);
                            }
                        }
                    }
                }
            }
        }
    }
}
